extension UIKeyboardType
{
    var tag:String{
        get{
            return "\(self)"
        }
    }
    var fk:[String:String] {
        get{
            return [FK.keyboardType:self.tag]
        }
    }
    
    static func type(with string:String) ->UIKeyboardType
    {
        switch string {
        case UIKeyboardType.numberPad.tag:
            return .numberPad
        case UIKeyboardType.phonePad.tag:
            return .phonePad
        case UIKeyboardType.decimalPad.tag:
            return .decimalPad
        case UIKeyboardType.asciiCapable.tag:
            return .asciiCapable
        case UIKeyboardType.emailAddress.tag:
            return .emailAddress
        case UIKeyboardType.URL.tag:
            return .URL
        case UIKeyboardType.default.tag:
            return .default
        default:
            return .default
        }
    }
    
    
}

extension FK{
    public enum Notice:String,NotifyType {
        case valueChange = "valueChange"
        case tableScroll = "tableScroll"
        
        public var name: Notification.Name{
            return "FK.\(Notice.self)\(self.rawValue)".notice_name
        }
    }
    
}

extension FK {
    
    

    //image上传相关
    public static func save()
    {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
    }
    
    public struct Img{
        public static var upload    = "update_path"
        public static var name      = "image_name"
        public static var node      = "image_node"
    }
    
    public static var SaveJoFormNotify = "SaveJoFormNotify"
    
    public static func placeHolder(field:Any?) -> String {
        var p = ""
        if let dict = field as? NSMutableDictionary
        {
            p = dict[FK.placeholder,""]
            if p == ""
            {
                if let rule = (dict[obj:FK.field,nil] as? StrField)?._rule
                {
                    p = rule.nil_msg
                }
            }
        }
        return p
    }
    
    public static func strField(field:Any?) -> StrField {
        if let dict = field as? NSMutableDictionary
        {
            if let f = dict[obj:FK.field,nil] as? StrField
            {
                
                return f
            }
        }
        return StrField(id: "err", defaultV: "err", need: false, hiden: true, rule: nil)
    }
    
    public static func fieldRule(field:Any?) -> JoRule? {
        if let dict = field as? NSMutableDictionary
        {
            if let rule = (dict[obj:FK.field,nil] as? StrField)?._rule
            {
                return rule
            }
        }
        return nil
    }
}



open class FieldView: JoView {
    
    public weak var controller:UIViewController? = nil
    
//    public var valueObsevers:[Any?] = [Any?]()
    
    public static var holderStyle:(color:UIColor,font:UIFont) = (UIColor._foregroundWeak,UIFont.systemFont(ofSize: 13))
    
    public static var valueStyle:(color:UIColor,font:UIFont) = (UIColor._foreground,UIFont.boldSystemFont(ofSize: 14))
    
    public var valueFont:UIFont = FieldView.valueStyle.font
    public var holderFont:UIFont = FieldView.holderStyle.font
    
    public var valueColor:UIColor = FieldView.valueStyle.color
    public var holderColor:UIColor = FieldView.holderStyle.color
    
    public var fieldNameL = UILabel()
    public var showNameLable = false
    
    open func forcus(){
        
    }
    
    public var ckecks:[String]{
        get{
            
            let __checks = mo.model[FK.checks,""]
            if __checks.count > 0
            {
                if __checks.contains(",")
                {
                    return __checks.components(separatedBy: ",")
                }else{
                    return [__checks]
                }
            }
            else{
                return []
            }
        }
    }


    public required init(frame: CGRect) {
        super.init(frame: frame)
        NotificationCenter.default.addObserver(self, selector: #selector(save_value), name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        addLayoutRules()
    }
    
   
    open var place_holder:String{
        return FK.placeHolder(field: mo.model)
    }
    
    open var rule:JoRule?{
        return FK.fieldRule(field: mo.model)
    }
    
    open var field:StrField{
        return FK.strField(field: mo.model)
    }
    
    
    public required init()
    {
        super.init(frame: [0])
        addLayoutRules()
        NotificationCenter.default.addObserver(self,selector: #selector(save_value),name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        NotificationCenter.default.addObserver(self, selector: #selector(save_value), name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
        addLayoutRules()
    }
    
    
    open override func mo_model_reload() {
        if let field = self.putItem()
        {
            self.mo.model[FK.field] = field
        }
        super.mo_model_reload()
        
    }
    
    @objc open func save_value()
    {
//        if let put = mo.model[obj:FK.field,nil] as? StrField
//        {
//            let str = self.get_string_value()
//            put._value = str
//        }
        
        _ = self.get_string_value()
        
    }
    
    open func check_value(value:String) ->(Bool,String?){
        return (true,nil)
    }
    
    
    @discardableResult
    @objc open func save(_ value:String)->Bool
    {
        if self.check_value(value: value).0
        {
            self.mo.model[FK.value] = value
            if let field = self.putItem()
            {
                let _id = field._ID
                __post(notify: FK.Notice.valueChange, object: self.controller, userInfo: ["__id":_id,"__value":get_string_value(),"__model__":mo.model])
            }
            return true
        }
        else{
            return false
        }
    }
    open func get_string_value() ->String
    {
        return self.mo.model[FK.value,""]
    }
    
    
    public var defaultField:StrField? = nil
    
    
    open func putItem() ->StrField?
    {
        if let put = mo.model[obj:FK.field,nil] as? StrField
        {
            return put
        }
        if let put = mo.model[obj:FK.field,nil] as? String
        {
            defaultField?._ID = put
            if let field = defaultField
            {
                mo.model[FK.field] = field
            }
            return defaultField
        }
        return nil
    }
    
    deinit
    {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: FK.SaveJoFormNotify), object: nil)
    }
    
    
}
